package com.intel.factorytest.activity;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.intel.factorytest.TestFileOp;
import com.intel.factorytest.cfparser.ConfigFileParseUtil;
import com.intel.factorytest.cfparser.concrete.AConfigFileParser;
import com.intel.factorytest.menuitem.ServiceTestsItemMenu;

public class ShowServiceResultActivity extends Activity {

    List testMenuItems = null;
    StringBuilder mFailedItems = new StringBuilder();
    private TextView mTextView;
    private String  tName;
    private File  sampleDir;
    private File  sampleDir1;
    private boolean  isFirst = true;
    private static final String  TAG = "ShowServiceResultActivity";
    Map<String, Object> testItem = new HashMap<String,Object>();
//    private String Sm_nvm_result ="/data/Linux/Marvell/NVM/result.txt";
//    private String Se_nvm_result ="/data/Linux/Marvell/NVM/result_s.txt";
    private TestFileOp mTestFile = new TestFileOp();
    private int isSys = 0;
    private ArrayList<Integer> testStatus = new ArrayList<Integer>();
    private String Sm_nvm_result ="/data/data/com.intel.factorytest/result.txt";
    private String Se_nvm_result ="/data/data/com.intel.factorytest/result_s.txt";

    @Override
    public void onCreate(Bundle savedInstanceState)
    {        
        super.onCreate(savedInstanceState);
        mTextView = new TextView(this);
        this.setContentView(mTextView);
        isFirst = IsFirstStatus(); 
        if (isFirst) {
            mFailedItems.append("The first test!"+"\n");
        }else {
            isSys = mTestFile.ReadTextFile();
            Log.e(TAG,"######### isSys ="+isSys);
//            testMenuItems = new ServiceTestsItemMenu(this).showTestsMenuMap(isSys, 0, "", "");
            testMenuItems = ConfigFileParseUtil.getConfParserConcrete(this, "").doParse(isSys, 0, "", "");
            initSensorItem();
            ReadResultFile(); 
        }
            getFailedItems();
    }
    private void getFailedItems () {
        mTextView.setText(mFailedItems.toString());
    }
    private void ReadResultFile() {
        BufferedReader reader=null;
        BufferedReader reader1=null;
        String s=null;
        int whichFail = 0,startIndex=0,endIndex=0;// 0 no fail 1 factorytest 2 sensor
        File smfile = new File(Sm_nvm_result);
        File ssfile = new File(Se_nvm_result);

        if (smfile.length()>0) {
            mFailedItems.append("FactoryTest last failed items are :"+"\n");
            startIndex = mFailedItems.indexOf("FactoryTest");
            endIndex = mFailedItems.lastIndexOf(":");
            Log.e(TAG,"startIndex  =="+startIndex+"endIndex="+endIndex);
            whichFail = 1;    
        } else if (smfile.length()==0) {
            testStatus.add(1);
        }

        try {
            if (whichFail != 0) {
                reader = new BufferedReader(new FileReader(Sm_nvm_result));
                while ((s = reader.readLine()) != null) {
                    if (s.equals("pass")) {
                        mFailedItems.replace(startIndex,endIndex+1,"FactoryTest last test is success "+"\n");
                        break;
                    }
                    Map map =  (Map) testMenuItems.get(Integer.valueOf(s));
                    tName = (String)map.get("itemName");    
                    mFailedItems.append(tName+"\n");
                    Log.e(TAG,"s =="+s+"tName ="+tName);
                }        
            }
            if (ssfile.length()>0) {
                mFailedItems.append("SensorTest last failed items are :"+"\n");
                startIndex = mFailedItems.indexOf("SensorTest");
                endIndex = mFailedItems.lastIndexOf(":");
                Log.e(TAG,"222startIndex  =="+startIndex+"endIndex="+endIndex);
                whichFail = 2;    
            } else if (ssfile.length()==0){
                testStatus.add(2);
            }
            if (whichFail == 2) {
                reader1 = new BufferedReader(new FileReader(Se_nvm_result));
                while ((s = reader1.readLine()) != null) {
                    if (s.equals("pass")) {
                        mFailedItems.replace(startIndex,endIndex+1,"Sensors last test is success "+"\n");
                        break;
                    }
                    tName = (String)testItem.get(s);
                    mFailedItems.append(tName+"\n");
                    Log.e(TAG," 222s =="+s+"tName ="+tName);
                }        
            }    
        } catch (IOException e) {
            Log.e(TAG,"read  result failed");
        }finally{
            try {
                if (reader != null) {
                    reader.close();
                }
                if (reader1 != null) {
                    reader1.close();
                }
                if (testStatus.contains(1)) {
                    mFailedItems.append("FactoryTest last test is not complete "+"\n");
                }
                if (testStatus.contains(2)) {
            //for nosensor test        mFailedItems.append("Sensors last test is not complete "+"\n");
                }
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }
        }
    }
    private boolean IsFirstStatus () {
        sampleDir = new File("/data/data/com.intel.factorytest/result.txt");
        sampleDir1 = new File("/data/data/com.intel.factorytest/result_s.txt");
        if ((sampleDir == null || !sampleDir.exists()) && ( sampleDir1 == null || !sampleDir1.exists()) ) {
            return true;

        } else {
            return false;
        }
    }
/*
      public void ReadResultFile() 
        {               
            String fileName="/dev/block/mmcblk0p2";
        int value = 0;
        int sum  = 0;
            int mSensorNum = 0;
        
            String isSensor = null;
        
        try                 
            {       
             DataInputStream  in=new DataInputStream(

                                   new BufferedInputStream(
                                   new FileInputStream(fileName)));
        sum  = in.readInt();
        mSensorNum = in.readInt(); 
        Log.e(TAG,"sum ="+sum+"mSensorNum ="+mSensorNum);
        if (sum > mSensorNum) {
            mFailedItems.append("FactoryTest Last Failed items are :"+"\n");
        }
        for (int i =0;i<(sum-mSensorNum);i++) {
            value = in.readInt();
            Log.e(TAG,"readMethod1  res ="+value);    
            Map map =  (Map) testMenuItems.get(value);
            tName = (String)map.get("itemName");    
            mFailedItems.append(tName+"\n");
        //    Log.e(TAG,"s =="+s+"tName ="+tName);
        }

        if (mSensorNum!=0) {
            mFailedItems.append("SensorTest Last Failed items are :"+"\n");
        }
        for (int i =0;i<mSensorNum;i++) {
            value = in.readInt();
            Log.e(TAG,"readMethod1  res ="+value);    
            tName = (String)testItem.get(Integer.toString(value));
            mFailedItems.append(tName+"\n");
        }
                in.close();      
 
        } catch (Exception e) 
            {  
                e.printStackTrace();      
            }      
        } 
*/
      private void initSensorItem ( ) {
        testItem.put("1","Accelerater");
        testItem.put( "2","Light");
      }
}

